package co.edu.usbcali.egrapico.vista.ambient;

import co.edu.usbcali.egrapico.vista.FrmPrincipal;
import co.edu.usbcali.egrapico.vista.tools.JPanelTranferHandlerPrecedentMsg;

public class PanelPrecedentMsg extends javax.swing.JInternalFrame implements java.io.Serializable {

    private int yScroll;
    LPaneRestrictionMsg layeredPane;
    javax.swing.JScrollPane scroll1;
    private javax.swing.JPanel panel2;
    private int valor = 0;
    private int variable = 0;
    private int argumento = 0;  //parametro
    private int sender = 0;
    private int metodo = 0;
    private int identi = 0;

    /**
     * Creates a new instance of PanelDragAndDrop
     */
    public PanelPrecedentMsg() {
        super("",//+(++openFrameCount) 
                true, //resizable
                true, //closable
                true, //maximizable
                true);//iconifiable

        yScroll = 30;
        scroll1 = new javax.swing.JScrollPane();
        scroll1.setAutoscrolls(true);
        scroll1.setBorder(javax.swing.BorderFactory.createTitledBorder("List of arguments"));
        layeredPane = new LPaneRestrictionMsg();
        scroll1.setViewportView(layeredPane);
        getContentPane().setLayout(new java.awt.GridLayout());
        panel2 = new javax.swing.JPanel();
        panel2.setLayout(null);
        panel2.setBackground(new java.awt.Color(255, 255, 255));
        panel2.setPreferredSize(new java.awt.Dimension(100, 280));
        panel2.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PMouseClicked(evt);
            }
        });
        getContentPane().add(scroll1);
        scroll1.setPreferredSize(new java.awt.Dimension(130, 280));
        getContentPane().add(panel2);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTransferHandler(new JPanelTranferHandlerPrecedentMsg());
        setSize(520, 300);
        //Set the window's location.
        setLocation(30, 30);
        setVisible(true);

    }

    public void loadFrame(PanelPrecedentMsg ob) {
        valor = ob.valor;
        variable = ob.variable;
        argumento = ob.argumento;  //parametro
        sender = ob.sender;
        metodo = ob.metodo;
        identi = ob.identi;
        layeredPane.setPreferredSize(ob.getLPane().getPreferredSize());
        layeredPane.setAreaLP(ob.getLPane().areaLPane());
        for (int i = 0; i < ob.getLPane().getComponentCount(); i++) {
            LabelIdeOpe label = new LabelIdeOpe();
            label.setIcon(((LabelIdeOpe) ob.getLPane().getComponent(i)).getIcon());
            label.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
            label.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
            label.setText(((LabelIdeOpe) ob.getLPane().getComponent(i)).getText());
            label.setId(((LabelIdeOpe) ob.getLPane().getComponent(i)).getId());
            label.setLocation(((LabelIdeOpe) ob.getLPane().getComponent(i)).getLocation());
            label.setSize(((LabelIdeOpe) ob.getLPane().getComponent(i)).getSize());
            label.setTextPane(((LabelIdeOpe) ob.getLPane().getComponent(i)).getTextPane());
            label.setTextToolTip(((LabelIdeOpe) ob.getLPane().getComponent(i)).getTextPane());
            layeredPane.addLabel(label);
            layeredPane.addLabeltoVector(label);
        }
        for (int i = 0; i < ob.getLPaneMethod().getComponentCount(); i++) {
            if (ob.getLPaneMethod().getComponent(i) instanceof LabelIdeOpe) {
                LabelIdeOpe label = new LabelIdeOpe();
                label.setIcon(((LabelIdeOpe) ob.getLPaneMethod().getComponent(i)).getIcon());
                label.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
                label.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
                label.setText(((LabelIdeOpe) ob.getLPaneMethod().getComponent(i)).getText());
                label.setId(((LabelIdeOpe) ob.getLPaneMethod().getComponent(i)).getId());
                label.setLocation(((LabelIdeOpe) ob.getLPaneMethod().getComponent(i)).getLocation());
                label.setSize(((LabelIdeOpe) ob.getLPaneMethod().getComponent(i)).getSize());
                label.setTextPane(((LabelIdeOpe) ob.getLPaneMethod().getComponent(i)).getTextPane());
                label.setTextToolTip(((LabelIdeOpe) ob.getLPaneMethod().getComponent(i)).getTextPane());
                panel2.add(label);
            } else {
                LabelMethod label = new LabelMethod();
                label.setIcon(((LabelMethod) ob.getLPaneMethod().getComponent(i)).getIcon());
                label.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
                label.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
                label.setText(((LabelMethod) ob.getLPaneMethod().getComponent(i)).getText());
                label.setLocation(((LabelMethod) ob.getLPaneMethod().getComponent(i)).getLocation());
                label.setSize(((LabelMethod) ob.getLPaneMethod().getComponent(i)).getSize());
                label.setTextPane(((LabelMethod) ob.getLPaneMethod().getComponent(i)).getTextPane());
                label.setTextToolTip(((LabelMethod) ob.getLPaneMethod().getComponent(i)).getTextPane());
                panel2.add(label);
            }
        }

    }

    public LPaneRestrictionMsg getLPane() {
        return layeredPane;
    }

    public javax.swing.JPanel getLPaneMethod() {
        return panel2;
    }

    public void addMyListenerToLPane() {
        setTransferHandler(new JPanelTranferHandlerPrecedentMsg());
        layeredPane.addMyListener();
    }

    public void addLayeredPane(LabelIdeOpe label) {
        if (label.getId().equals("id2")) {
            if (label.getName().equals("Value")) {
                label.setName(label.getName() + (++valor));
                label.setText(label.getName());
                label.setTextToolTip(label.getName() + ": ");
            } else if (label.getName().equals("Variable")) {
                label.setName(label.getName() + (++variable));
                label.setText(label.getName());
                label.setTextToolTip(label.getName() + ": ");
            } else if (label.getName().equals("Argument")) {
                label.setName(label.getName() + (++argumento));
                label.setText(label.getName());
                label.setTextToolTip(label.getName() + ": ");
            } else if (label.getName().equals("Sender")) {
                label.setName(label.getName() + (++sender));
                label.setText(label.getName());
                label.setTextToolTip(label.getName() + ": ");
            }

            layeredPane.addLabel(label);
            layeredPane.addLabeltoVector(label);
        } else if (label.getId().equals("id4")) {
            label.setText(label.getName());
            panel2.add(label);
            panel2.repaint();
            identi++;
        }
        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    private void PMouseClicked(java.awt.event.MouseEvent evt) {
        java.awt.Component c = panel2.findComponentAt(evt.getX(), evt.getY());
        if (evt.getButton() == java.awt.event.MouseEvent.BUTTON3) {
            if (c instanceof LabelIdeOpe) {
                ((LabelIdeOpe) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            } else if (c instanceof LabelMethod) {
                ((LabelMethod) c).mostrarMenu(evt.getComponent(), evt.getX(), evt.getY());
            }
        } else if (evt.getClickCount() == 2) {
            if (c instanceof LabelIdeOpe) {
                if (((LabelIdeOpe) c).isCreate() == false) {
                    ((LabelIdeOpe) c).Ladicionar();
                } else {
                    ((LabelIdeOpe) c).mostrarPanel();
                }

            }
        }
    }

    public void addLayeredPane(LabelMethod label) {
        javax.swing.ImageIcon I12 = new javax.swing.ImageIcon(getClass().getResource("/co/edu/usbcali/egrapico/images/argumentP.gif"));
        for (int i = 1; i <= FrmPrincipal.getMethod(label.getName()).getFrame().getArguments(); i++) {
            LabelIdeOpe labelArg = new LabelIdeOpe();
            labelArg.setIcon(I12);
            labelArg.setHorizontalTextPosition(javax.swing.JLabel.CENTER);
            labelArg.setVerticalTextPosition(javax.swing.JLabel.BOTTOM);
            labelArg.setSize(80, 80);
            labelArg.setId("id2");
            labelArg.setName(FrmPrincipal.getMethod(label.getName()).getFrame().getArguments(i));
            labelArg.setText(labelArg.getName());
            labelArg.setTextToolTip(labelArg.getName() + ": ");
            labelArg.setLocation(30, i * 100 - 70);
            layeredPane.setSize(220, layeredPane.getHeight() + 80);
            layeredPane.scrollRectToVisible(new java.awt.Rectangle(0, 0, 220, layeredPane.getHeight() + 80));
            layeredPane.setPreferredSize(new java.awt.Dimension(220, layeredPane.getHeight() + 80));
            layeredPane.addLabel(labelArg);
        }
        label.setText(label.getName());
        panel2.add(label);
        panel2.repaint();
        metodo++;

        try {
            setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
    }

    public LPaneRestrictionMsg getLayered() {
        return layeredPane;
    }

    public void setUndoLPane() {
        layeredPane.Undo();
    }

    public void setRedoLPane() {
        layeredPane.Redo();
    }

    public int getRedosize() {
        return layeredPane.getRedoSize();
    }

    public int getUndosize() {
        return layeredPane.getUndoSize();
    }

    public int getYscroll() {
        return yScroll;
    }

    public void setYscroll() {
        yScroll = yScroll + 100;
    }

    public int getPuertos() {
        return valor + variable + argumento + sender;
    }

    public int getidentificador() {
        return identi;
    }

    public void resIdenti() {
        identi--;
    }

    public int getmethodCount() {
        return metodo;
    }

    public void resmethod() {
        metodo--;
    }

    public void resetPuertos() {
        valor = 0;
        variable = 0;
        argumento = 0;
        sender = 0;
    }

    public void resValue() {
        valor--;
    }

    public void resVariable() {
        variable--;
    }

    public void resArgumento() {
        argumento--;
    }

    public void resSenderOperador() {
        sender--;
    }

    public LabelIdeOpe getIdObject() {
        if (panel2.getComponent(0) instanceof LabelIdeOpe) {
            return (LabelIdeOpe) panel2.getComponent(0);
        } else if (panel2.getComponent(1) instanceof LabelIdeOpe) {
            return (LabelIdeOpe) panel2.getComponent(1);
        } else {
            return null;
        }
    }

    public LabelMethod getMethodObject() {
        if (panel2.getComponent(0) instanceof LabelMethod) {
            return (LabelMethod) panel2.getComponent(0);
        } else if (panel2.getComponent(1) instanceof LabelMethod) {
            return (LabelMethod) panel2.getComponent(1);
        } else {
            return null;
        }
    }
}